package com.pactera.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class DateUtil {

	/**
	 * 获得昨天日期
	 * 
	 * @return 2012-10-17 14:59:03
	 */
	public static String getYesterdayDate() {

		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.DATE, -1);
		String yesterday = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
				.format(cal.getTime());
		return yesterday;
	}

	/**
	 * 今天日期
	 * 
	 * @return 2012-10-18 14:59:03
	 */
	public static String getCurrentDate() {

		Calendar cal = Calendar.getInstance();
		String yesterday = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
				.format(cal.getTime());
		return yesterday;
	}

	/**
	 * 获得明天日期
	 * 
	 * @return 2012-10-19 14:59:03
	 */
	public static String getTomorrowDate() {

		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.DATE, 1);
		String yesterday = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
				.format(cal.getTime());
		return yesterday;
	}

	/**
	 * 此方法有问题，应使用服务器时间
	 * @param beginTime
	 *            起始时间
	 * @param endTime
	 *            结束时间
	 * @return -1 等待直播 0 正在直播 1 直播完毕 -2 错误
	 * @throws ParseException
	 */
	public static byte CompareTime(String beginTime, String endTime) {
		try {
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date ndate = new Date();
			Date begin = df.parse(beginTime);
			Date end = df.parse(endTime);
			if ((begin.getTime() < ndate.getTime())
					&& (end.getTime() > ndate.getTime())) {
				return 0;
			} else if (end.getTime() < ndate.getTime()) {
				return 1;
			} else if (begin.getTime() > ndate.getTime()) {
				return -1;
			} else {
				return -2;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return -2;
		}
	}

	/**
	 * 比较两个时间
	 * 
	 * @param one
	 * @param another
	 * @return 1 one 时间比较大 -1 another 时间比较大
	 */
	public static byte CompareTime1(String one, String another) {
		try {
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date oneDate = df.parse(one);
			Date anotherDate = df.parse(another);
			if (oneDate.getTime() > anotherDate.getTime()) {
				return 1;
			} else if (oneDate.getTime() < anotherDate.getTime()) {
				return -1;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0;
	}

	/**
	 * 时间格式转换  "MM月dd日"
	 * 
	 * @param time
	 *            2012-05-18 15:14:03
	 * @return 5月18日
	 */
	public static String formatDate(String time) {
		String formatStr;
		try {
			SimpleDateFormat fomat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date date = fomat.parse(time);
			fomat = new SimpleDateFormat("MM月dd日");
			formatStr = fomat.format(date);
			if (formatStr.startsWith("0")) {
				formatStr = formatStr.substring(1);
			}
		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		}
		return formatStr;
	}

	/**
	 * 时间格式转换 "hh:mm"
	 * 
	 * @param time
	 *            2012-05-18 15:14:03
	 * @return 3:14PM
	 */
	public static String format1Date(String time) {
		String formatStr;
		Calendar calendar = new GregorianCalendar();
		try {
			SimpleDateFormat fomat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date date = fomat.parse(time);
			calendar.setTime(date);
			fomat = new SimpleDateFormat("hh:mm");
			formatStr = fomat.format(date);
			if (formatStr.startsWith("0")) {
				formatStr = formatStr.substring(1);
			}
			String ampmValues;
			if (calendar.get(Calendar.AM_PM) == 0) {
				ampmValues = "AM";
			} else {
				ampmValues = "PM";
			}
			formatStr += ampmValues;
		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		}
		return formatStr;
	}

	public static String format2Date(String beginTime, String endTime) {
		String formatStr = null;
		String toStr = null;
		SimpleDateFormat fomat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date begin = null;
		Date end = null;
		try {
			begin = fomat.parse(beginTime);
			end = fomat.parse(endTime);
			fomat = new SimpleDateFormat("HH:mm");
			formatStr = fomat.format(begin);
			toStr = fomat.format(end);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return formatStr + "-" + toStr;
	}

	public static boolean compare3Date(String time) {
		String formatStr;
		String nowStr;
		try {
			SimpleDateFormat fomat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date date = fomat.parse(time);
			Date now = new Date();
			fomat = new SimpleDateFormat("HH:mm");

			formatStr = fomat.format(date);
			nowStr = fomat.format(now);
			if (formatStr.equals(nowStr)) {
				return true;
			} else {
				return false;
			}

		} catch (ParseException e) {
			e.printStackTrace();
			return false;
		}
	}

	/**
	 * 获取当前日期是周几<br>
	 * 
	 * @param dt
	 * @return 当前日期是周几
	 */
	public static String getWeekOfDate(String time) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date dt = null;
		try {
			dt = df.parse(time);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		String[] weekDays = { "周日", "周一", "周二", "周三", "周四", "周五", "周六" };
		Calendar cal = Calendar.getInstance();
		cal.setTime(dt);

		int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
		if (w < 0)
			w = 0;

		return weekDays[w];
	}

}
